﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
//
using System.IO;
using System.Configuration;
/*
Copyright 2010 Mirosław M. Mączka

This file is part of Mp3Cleaner.

    Mp3Cleaner is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    Foobar is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Foobar; if not, write to the Free Software
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA

*/
namespace Mp3Edit
{
    public partial class Form1 : Form
    {
        //
        public BindingList<CData> LD = new BindingList<CData>();
        //
        //public static string StartDirector y = @"D:\INET\USB\ok\Caterina Valente-Ok_nt album";
        public static string[] DefaultMusicFilesExtensions = { @".mp3", @".wma", @".ogg", @".mp4", @".m4a", @".wav", @".flac", @".ra", @".ape", @".mpc", @"" };
        public static string[] MusicFilesExtensions = DefaultMusicFilesExtensions;//należy przywrócić default przed odczytem katalogu
        //public static string StartDirectory = @"D:\INET\USB\ok\Madredeus - Faluas Do Tejo (2005) - World";
        public static string StartDirectory = @"";
        //
        // Struktura katalogów: .../Artist/Album/*.mp3
        public static string DirAlbum = "";// [Year] Album
        public static string DirArtist = ""; // Artist
        //
        // ustawiany, gdy numer ścieżki został * x00
        public bool isTrackMuliplied = false;
        //
        private bool exitAfterWrite = false;// do użycia w batchu
        private bool fullScreen = false;// do użycia w batchu
        //
        //
        //
        private int dragRow = 0;
        private int dragCol = 0;
        //
        //
        //
        public Form1()
        {
            //
            Utils.RenameHelpFiles();
            //
            //System.Threading.Thread.CurrentThread.CurrentUICulture = System.Globalization.CultureInfo.CreateSpecificCulture("");
            //
            InitializeComponent();
            //
            checkFirstTime();
            //
            dataGridView1.DataSource = LD;
            //bindingSource1.DataSource = LD;
            //
            this.Text = getVersionInfo();
            //
            if (!Utils.isEmpty(Environment.GetEnvironmentVariable("EXIT-AFTER-WRITE")))
            {
                exitAfterWrite = true;
            }
            if (!Utils.isEmpty(Environment.GetEnvironmentVariable("FULL-SCREEN")))
            {
                fullScreen = true;
            }
            //
            string[] cmdArgs = Environment.GetCommandLineArgs();
            string s = Environment.CommandLine;
            //MusicFilesExtensions = DefaultMusicFilesExtensions;
            if (2 == cmdArgs.GetLength(0))
            {
                string targetDir = cmdArgs[1];
                if (!Directory.Exists(targetDir))//zapewne to plik
                {
                    MusicFilesExtensions = new string[] { Path.GetExtension(targetDir) };
                    targetDir = Path.GetDirectoryName(targetDir);
                }
                if (Directory.Exists(targetDir))
                {
                    StartDirectory = targetDir;
                    WaitingTooLong wtl = new WaitingTooLong(5);
                    this.Enabled = false;
                    loadDir();
                    this.Enabled = true;
                    wtl.isTooLongBeep(this.Handle);
                    afterLoad(true);
                }
                setInfoFromPath(StartDirectory);
            }
            else
            {
                // informacja dla ukrytego przycisku [Navigate]
                StartDirectory = Properties.Settings.Default.pathNavigate;
            }
            //
            //
            if (fullScreen)
            {
                this.TopMost = true;
                this.WindowState = FormWindowState.Maximized;
            }
            // sortowanie według bazy danych
            //dataGridView1.Columns["fileTrackDataGridViewTextBoxColumn"].SortMode = DataGridViewColumnSortMode.Programmatic;
            //dataGridView1.Columns["fileTrackDataGridViewTextBoxColumn"].HeaderCell.SortGlyphDirection = SortOrder.Ascending;
            //
            //this.toolTip1.SetToolTip(this, "toolTip");
            //
        }

        private void Form1_FormClosed(object sender, FormClosedEventArgs e)
        {
            Properties.Settings.Default.Save();
        }

        private void dataGridView1_MouseDown(object sender, MouseEventArgs e)
        {
            //
            int rowIndex = dataGridView1.HitTest(e.X, e.Y).RowIndex;
            int colIndex = dataGridView1.HitTest(e.X, e.Y).ColumnIndex;
            //
            // dla drop, źeódło danych, inaczej po sortowaniu coś jest nie tak
            dragRow = rowIndex;
            dragCol = colIndex;
            //
            if (e.Button != MouseButtons.Left)
            {
                return;
            }
            //
            if ((colIndex > -1) && (rowIndex > -1))
            {

                dataGridView1.DoDragDrop(dataGridView1.Rows[rowIndex].Cells[colIndex].Value, DragDropEffects.Copy);
                //dataGridView1.DoDragDrop(LD[rowIndex][colIndex], DragDropEffects.Copy);
            }
            else if ((colIndex > -1) && (rowIndex == -1))
            {
                dataGridView1.DoDragDrop(colIndex, DragDropEffects.Copy);
            }
        }

        private void dataGridView1_DragDrop(object sender, DragEventArgs e)
        {
            if (e.Data.GetDataPresent(DataFormats.Text))
            {
                e.Effect = DragDropEffects.Copy;
                //
                Point clientPoint = dataGridView1.PointToClient(new Point(e.X, e.Y));
                int rowIndex = dataGridView1.HitTest(clientPoint.X, clientPoint.Y).RowIndex;
                int colIndex = dataGridView1.HitTest(clientPoint.X, clientPoint.Y).ColumnIndex;
                //
                //
                if ((colIndex > -1) && (rowIndex > -1))
                {
                    if (!(dataGridView1.Columns[colIndex].ReadOnly || !dataGridView1.Columns[colIndex].Visible))
                    {
                        //dataSet11.DirTable1[rowIndex][colIndex] = ((string)e.Data.GetData(DataFormats.UnicodeText));
                        //dataGridView1[dragCol][dropR
                        string d = (string)e.Data.GetData("UnicodeText", true);
                        //string d =dataGridView1.Rows[dragRow].Cells[dragCol].Value.ToString().Trim();
                        //string d = ((string)e.Data.GetData(DataFormats.UnicodeText));
                        if (null == d) return;
                        if ("" == d) return;
                        switch (CData.ColumnName(colIndex))
                        {
                            case "FileName":
                                this.dataGridView1.Rows[rowIndex].Cells[colIndex].Value = Utils.CleanFileName(d);
                                dataGridView1.Rows[rowIndex].Cells["fileNameChangedDataGridViewCheckBoxColumn"].Value = true;
                                dataGridView1.Rows[rowIndex].Cells["markerDataGridViewCheckBoxColumn"].Value = true;
                                break;
                            case "FileTrack":
                                this.dataGridView1.Rows[rowIndex].Cells[colIndex].Value = verifyNumber(Utils.CleanDrop(d),3);
                                //
                                dataGridView1.Rows[rowIndex].Cells["fileNameChangedDataGridViewCheckBoxColumn"].Value = true;
                                dataGridView1.Rows[rowIndex].Cells["fileTrackChangedDataGridViewCheckBoxColumn"].Value = true;// nie używane
                                dataGridView1.Rows[rowIndex].Cells["markerDataGridViewCheckBoxColumn"].Value = true;
                                break;
                            case "tagTrack":
                                this.dataGridView1.Rows[rowIndex].Cells[colIndex].Value = verifyNumber(Utils.CleanDrop(d),3);
                                //
                                dataGridView1.Rows[rowIndex].Cells["tagTrackChangedDataGridViewCheckBoxColumn"].Value = true;
                                dataGridView1.Rows[rowIndex].Cells["markerDataGridViewCheckBoxColumn"].Value = true;
                                break;
                            case "tagTitle":
                                this.dataGridView1.Rows[rowIndex].Cells[colIndex].Value = Utils.CleanDrop(d);
                                //
                                dataGridView1.Rows[rowIndex].Cells["tagTitleChangedDataGridViewCheckBoxColumn"].Value = true;
                                dataGridView1.Rows[rowIndex].Cells["markerDataGridViewCheckBoxColumn"].Value = true;
                                break;
                            case "tagArtist":
                                this.dataGridView1.Rows[rowIndex].Cells[colIndex].Value = Utils.CleanDrop(d);
                                //
                                dataGridView1.Rows[rowIndex].Cells["tagArtistChangedDataGridViewCheckBoxColumn"].Value = true;
                                dataGridView1.Rows[rowIndex].Cells["markerDataGridViewCheckBoxColumn"].Value = true;
                                break;
                            case "tagAlbum":
                                this.dataGridView1.Rows[rowIndex].Cells[colIndex].Value = Utils.CleanDrop(d);
                                //
                                dataGridView1.Rows[rowIndex].Cells["tagAlbumChangedDataGridViewCheckBoxColumn"].Value = true;
                                dataGridView1.Rows[rowIndex].Cells["markerDataGridViewCheckBoxColumn"].Value = true;
                                break;
                            case "tagYear":
                                this.dataGridView1.Rows[rowIndex].Cells[colIndex].Value = verifyNumber(Utils.CleanDrop(d),4);
                                //
                                dataGridView1.Rows[rowIndex].Cells["tagYearChangedDataGridViewCheckBoxColumn"].Value = true;
                                dataGridView1.Rows[rowIndex].Cells["markerDataGridViewCheckBoxColumn"].Value = true;
                                break;
                            case "tagGenre":
                                dataGridView1.Rows[rowIndex].Cells[colIndex].Value = Utils.CleanDrop(d);
                                //
                                dataGridView1.Rows[rowIndex].Cells["tagGenreChangedDataGridViewCheckBoxColumn"].Value = true;
                                dataGridView1.Rows[rowIndex].Cells["markerDataGridViewCheckBoxColumn"].Value = true;
                                break;
                        }
                    }
                }
                else if (-1 == rowIndex)// drop na nagłówek
                {
                    string d = ((string)e.Data.GetData(DataFormats.UnicodeText));
                    if (null == d) return;
                    if ("" == d) return;
                    switch (CData.ColumnName(colIndex))
                    {
                        case "tagArtist":
                            SetTagArtist(Utils.CleanDrop(d));
                            break;
                        case "tagAlbum":
                            SetTagAlbum(Utils.CleanDrop(d));
                            break;
                        case "tagYear":
                            SetTagYear(verifyNumber(Utils.CleanDrop(d),4));
                            break;
                        case "tagGenre":
                            SetTagGenre(Utils.CleanDrop(d));
                            break;
                    }
                }
                //
            }
            else if (e.Data.GetDataPresent(DataFormats.FileDrop))
            {
                e.Effect = DragDropEffects.Copy;
                //
                string targetDir = ((string[])e.Data.GetData(DataFormats.FileDrop))[0];
                //
                if (".CUE" == Path.GetExtension(targetDir).ToUpper())
                {
                    ReadCueFile rcf = new ReadCueFile();
                    if (rcf.ReadCue(targetDir))
                    {
                        if (0 != rcf.TRACKS.Length)
                        {
                            for (int i = 0; i < rcf.TRACKS.Length; i++)
                            {
                                SetTagTitleAuthor(i, rcf.TRACKS[i].TITLE, rcf.TRACKS[i].PERFORMER);
                            }
                            if ("" != rcf.TITLE)
                            {
                                SetTagAlbum(rcf.TITLE);
                            }
                            if ("" != rcf.PERFORMER)
                            {
                                SetTagArtist(rcf.PERFORMER);
                            }
                            if ("" != rcf.DATE)
                            {
                                SetTagYear(rcf.DATE);
                            }
                        }
                    }
                }
                else
                {
                    MusicFilesExtensions = DefaultMusicFilesExtensions;
                    //
                    if (!Directory.Exists(targetDir))//zapewne to plik
                    {
                        MusicFilesExtensions = new string[] { Path.GetExtension(targetDir) };
                        targetDir = Path.GetDirectoryName(targetDir);
                    }
                    if (Directory.Exists(targetDir))
                    {
                        StartDirectory = targetDir;
                        WaitingTooLong wtl = new WaitingTooLong(5);
                        loadDir();
                        wtl.isTooLongBeep(this.Handle);
                        afterLoad(true);
                        setInfoFromPath(StartDirectory);
                    }
                }
                //
            }
            else
            {
                if (1 == e.Data.GetFormats().Length)
                {
                    if ("System.Int32" == e.Data.GetFormats()[0])
                    {
                        Point clientPoint = dataGridView1.PointToClient(new Point(e.X, e.Y));
                        int rowIndex = dataGridView1.HitTest(clientPoint.X, clientPoint.Y).RowIndex;
                        int colIndex = dataGridView1.HitTest(clientPoint.X, clientPoint.Y).ColumnIndex;
                        //
                        if ((colIndex > -1) && (rowIndex == -1))
                        {
                            e.Effect = DragDropEffects.Copy;
                            int srcRow = (int)e.Data.GetData("System.Int32");
                            switch (CData.ColumnName(srcRow))
                            {
                                case "FileTrack":
                                    if ("tagTrack" == CData.ColumnName(colIndex))
                                    {
                                        Copy_FileTrack_2_tagTrack();
                                    }
                                    break;
                                case "FileName":
                                    if ("tagTitle" == CData.ColumnName(colIndex))
                                    {
                                        Copy_FileName_2_tagTitle();
                                    }
                                    break;
                                case "tagTrack":
                                    if ("FileTrack" == CData.ColumnName(colIndex))
                                    {
                                        Copy_tagTrack_2_FileTrack();
                                    }
                                    break;
                                case "tagTitle":
                                    if ("FileName" == CData.ColumnName(colIndex))
                                    {
                                        Copy_tagTitle_2_FileName();
                                    }
                                    break;
                                default:
                                    e.Effect = DragDropEffects.None;
                                    break;
                            }
                        }
                    }
                }
                e.Effect = DragDropEffects.None;
            }
            //
            dataGridView1.Refresh();
            //
        }

        private void dataGridView1_DragEnter(object sender, DragEventArgs e)
        {
            e.Effect = DragDropEffects.Copy;
        }

        private void dataGridView1_DragLeave(object sender, EventArgs e)
        {

        }

        private void dataGridView1_DragOver(object sender, DragEventArgs e)
        {
            e.Effect = DragDropEffects.Copy;
        }

        private void dataGridView1_CellValidating(object sender, DataGridViewCellValidatingEventArgs e)
        {
            //string s = dataGridView1.Columns[e.ColumnIndex].Name;
            switch (dataGridView1.Columns[e.ColumnIndex].Name)
            {
                case "fileNameDataGridViewTextBoxColumn":
                    dataGridView1.Rows[e.RowIndex].Cells["fileNameDataGridViewTextBoxColumn"].Value = e.FormattedValue.ToString();
                    dataGridView1.Rows[e.RowIndex].Cells["fileNameChangedDataGridViewCheckBoxColumn"].Value = true;
                    dataGridView1.Rows[e.RowIndex].Cells["markerDataGridViewCheckBoxColumn"].Value = true;
                    break;
                case "fileTrackDataGridViewTextBoxColumn":
                    dataGridView1.Rows[e.RowIndex].Cells["fileTrackDataGridViewTextBoxColumn"].Value = e.FormattedValue.ToString();
                    dataGridView1.Rows[e.RowIndex].Cells["fileTrackChangedDataGridViewCheckBoxColumn"].Value = true;
                    dataGridView1.Rows[e.RowIndex].Cells["markerDataGridViewCheckBoxColumn"].Value = true;
                    break;
                case "tagTrackDataGridViewTextBoxColumn":
                    dataGridView1.Rows[e.RowIndex].Cells["tagTrackDataGridViewTextBoxColumn"].Value = e.FormattedValue.ToString();
                    dataGridView1.Rows[e.RowIndex].Cells["tagTrackChangedDataGridViewCheckBoxColumn"].Value = true;
                    dataGridView1.Rows[e.RowIndex].Cells["markerDataGridViewCheckBoxColumn"].Value = true;
                    checkBox_UpdateTags.Checked = true;
                    break;
                case "tagTitleDataGridViewTextBoxColumn":
                    dataGridView1.Rows[e.RowIndex].Cells["tagTitleDataGridViewTextBoxColumn"].Value = e.FormattedValue.ToString();
                    dataGridView1.Rows[e.RowIndex].Cells["tagTitleChangedDataGridViewCheckBoxColumn"].Value = true;
                    dataGridView1.Rows[e.RowIndex].Cells["markerDataGridViewCheckBoxColumn"].Value = true;
                    checkBox_UpdateTags.Checked = true;
                    break;
                case "tagArtistDataGridViewTextBoxColumn":
                    dataGridView1.Rows[e.RowIndex].Cells["tagArtistDataGridViewTextBoxColumn"].Value = e.FormattedValue.ToString();
                    dataGridView1.Rows[e.RowIndex].Cells["tagArtistChangedDataGridViewCheckBoxColumn"].Value = true;
                    dataGridView1.Rows[e.RowIndex].Cells["markerDataGridViewCheckBoxColumn"].Value = true;
                    checkBox_UpdateTags.Checked = true;
                    break;
                case "tagAlbumDataGridViewTextBoxColumn":
                    dataGridView1.Rows[e.RowIndex].Cells["tagAlbumDataGridViewTextBoxColumn"].Value = e.FormattedValue.ToString();
                    dataGridView1.Rows[e.RowIndex].Cells["tagAlbumChangedDataGridViewCheckBoxColumn"].Value = true;
                    dataGridView1.Rows[e.RowIndex].Cells["markerDataGridViewCheckBoxColumn"].Value = true;
                    checkBox_UpdateTags.Checked = true;
                    break;
                case "tagYearDataGridViewTextBoxColumn":
                    dataGridView1.Rows[e.RowIndex].Cells["tagYearDataGridViewTextBoxColumn"].Value = e.FormattedValue.ToString();
                    dataGridView1.Rows[e.RowIndex].Cells["tagYearChangedDataGridViewCheckBoxColumn"].Value = true;
                    dataGridView1.Rows[e.RowIndex].Cells["markerDataGridViewCheckBoxColumn"].Value = true;
                    checkBox_UpdateTags.Checked = true;
                    break;
                case "tagGenreDataGridViewTextBoxColumn":
                    dataGridView1.Rows[e.RowIndex].Cells["tagGenreDataGridViewTextBoxColumn"].Value = e.FormattedValue.ToString();
                    dataGridView1.Rows[e.RowIndex].Cells["tagGenreChangedDataGridViewCheckBoxColumn"].Value = true;
                    dataGridView1.Rows[e.RowIndex].Cells["markerDataGridViewCheckBoxColumn"].Value = true;
                    checkBox_UpdateTags.Checked = true;
                    break;
            }

        }

        private void buttonNavigate_Click(object sender, EventArgs e)
        {
            folderBrowserDialog1.ShowNewFolderButton = false;
            folderBrowserDialog1.Description = "mp3";
            StartDirectory = Utils.GetPathRegressed(StartDirectory);
            if (string.Empty != StartDirectory)
            {
                folderBrowserDialog1.RootFolder = System.Environment.SpecialFolder.MyComputer;
                folderBrowserDialog1.SelectedPath = StartDirectory;
            }
            else
            {
                folderBrowserDialog1.RootFolder = System.Environment.SpecialFolder.MyMusic;
                folderBrowserDialog1.SelectedPath = @"";
            }
            //
            try
            {
                if (DialogResult.OK == folderBrowserDialog1.ShowDialog())// null reference czasami
                {
                    StartDirectory = folderBrowserDialog1.SelectedPath;
                    Properties.Settings.Default.pathNavigate = StartDirectory;
                    //
                    setInfoFromPath(StartDirectory);
                    //
                    MusicFilesExtensions = DefaultMusicFilesExtensions;
                    WaitingTooLong wtl = new WaitingTooLong(5);
                    this.Enabled = false;
                    loadDir();
                    this.Enabled = true;
                    wtl.isTooLongBeep(this.Handle);
                    afterLoad(true);
                }
            }
            catch (Exception esd)
            {
                MessageBox.Show(esd.Message);
            }
        }

        private void buttonReload_Click(object sender, EventArgs e)
        {
            if (Directory.Exists(StartDirectory))
            {
                WaitingTooLong wtl = new WaitingTooLong(5);
                this.Enabled = false;
                loadDir();
                this.Enabled = true;
                wtl.isTooLongBeep(this.Handle);
                afterLoad(false);
            }
            //
            dataGridView1.Refresh();
        }

        private void buttonDoClean_Click(object sender, EventArgs e)
        {
            DoClean();
            afterClean();
        }

        private void buttonWrite_Click(object sender, EventArgs e)
        {
            WaitingTooLong wtl = new WaitingTooLong(5);
            this.Enabled = false;
            DoWrite();
            this.Enabled = true;
            wtl.isTooLongBeep(this.Handle);
            afterWrite();
            if (exitAfterWrite)
            {
                //do użycia w batchu, katalog po katalogu, z listy
                this.Close();
            }
            //
            dataGridView1.Refresh();
        }

        private void buttonRemove_Click(object sender, EventArgs e)
        {
            DoRemove();
        }

        private void buttonCpTrack_Click(object sender, EventArgs e)
        {
            Copy_tagTrack_2_FileTrack();
        }

        private void buttonCpName_Click(object sender, EventArgs e)
        {
            Copy_tagTitle_2_FileName();
        }

        private void buttonCombineTagTitle_Click(object sender, EventArgs e)
        {
            if (0 == (Control.ModifierKeys & Keys.Shift))
            {
                Concat_tagArtisttagTitle2FileName(false);
            }
            else
            {
                Concat_tagArtisttagTitle2FileName(true);
            }
        }

        private void buttonSplitTagTitle_Click(object sender, EventArgs e)
        {
            if (0 == (Control.ModifierKeys & Keys.Shift))
            {
                Split_tagTitle2tagTitletagAuthor(false);
            }
            else
            {
                Split_tagTitle2tagTitletagAuthor(true);
            }
        }

        private void buttonSetTagArtist_Click(object sender, EventArgs e)
        {
            string artist = CaseUtils.ToCamelIfNeeded(DirArtist);
            //
            artist = Utils.swapThe(artist);
            //
            InputBox.InputBoxResult ibr = InputBox.Show("Artist:", "tagArtist", artist);
            if (!ibr.OK) return;
            //
            SetTagArtist(ibr.Text);
        }

        private void buttonSetTagAlbum_Click(object sender, EventArgs e)
        {
            string album = CaseUtils.ToCamelIfNeeded(Utils.GetWithoutYearFromString(DirAlbum).Trim());
            //
            InputBox.InputBoxResult ibr = InputBox.Show("album:", "tagAlbum", album);
            if (!ibr.OK) return;
            
            SetTagAlbum(ibr.Text);
        }

        private void buttonSetTagYear_Click(object sender, EventArgs e)
        {
            string year = Utils.GetYearFromString(DirAlbum);
            //
            InputBox.InputBoxResult ibr = InputBox.Show("Year:", "tagYear", year);
            if (!ibr.OK) return;
            //
            SetTagYear(verifyNumber(ibr.Text,4));
        }

        private void buttonSetTagGenre_Click(object sender, EventArgs e)
        {
            string genre = "";
            //
            InputBox.InputBoxResult ibr = InputBox.Show("Genre:", "tagGenre", genre);
            if (!ibr.OK) return;
            //
            SetTagGenre(ibr.Text);
        }

        private void buttonSetTagArtist_DragDrop(object sender, DragEventArgs e)
        {
            if (e.Data.GetDataPresent(DataFormats.Text))
            {
                //artist = Utils.swapThe(artist);// chyba nie
                SetTagArtist(Utils.CleanDrop((string)e.Data.GetData(DataFormats.UnicodeText)));
            }
        }

        private void buttonSetTagArtist_DragEnter(object sender, DragEventArgs e)
        {
            e.Effect = DragDropEffects.Copy;
        }

        private void buttonSetTagArtist_DragOver(object sender, DragEventArgs e)
        {
            e.Effect = DragDropEffects.Copy;
        }

        private void buttonSetTagAlbum_DragDrop(object sender, DragEventArgs e)
        {
            if (e.Data.GetDataPresent(DataFormats.Text))
            {
                SetTagAlbum(Utils.CleanDrop((string)e.Data.GetData(DataFormats.UnicodeText)));
            }
        }

        private void buttonSetTagAlbum_DragEnter(object sender, DragEventArgs e)
        {
            e.Effect = DragDropEffects.Copy;
        }

        private void buttonSetTagAlbum_DragOver(object sender, DragEventArgs e)
        {
            e.Effect = DragDropEffects.Copy;
        }

        private void buttonSetTagYear_DragDrop(object sender, DragEventArgs e)
        {
            if (e.Data.GetDataPresent(DataFormats.Text))
            {
                SetTagYear(verifyNumber(Utils.CleanDrop((string)e.Data.GetData(DataFormats.UnicodeText)),4));
            }
        }

        private void buttonSetTagYear_DragEnter(object sender, DragEventArgs e)
        {
            e.Effect = DragDropEffects.Copy;
        }

        private void buttonSetTagYear_DragOver(object sender, DragEventArgs e)
        {
            e.Effect = DragDropEffects.Copy;
        }

        private void buttonSetTagGenre_DragDrop(object sender, DragEventArgs e)
        {

            if (e.Data.GetDataPresent(DataFormats.Text))
            {
                SetTagGenre(Utils.CleanDrop((string)e.Data.GetData(DataFormats.UnicodeText)));
            }
            //
            dataGridView1.Refresh();
        }

        private void buttonSetTagGenre_DragEnter(object sender, DragEventArgs e)
        {
            e.Effect = DragDropEffects.Copy;
        }

        private void buttonSetTagGenre_DragOver(object sender, DragEventArgs e)
        {
            e.Effect = DragDropEffects.Copy;
        }

        private void button2tagTrack_Click(object sender, EventArgs e)
        {
            Copy_FileTrack_2_tagTrack();
            //
            dataGridView1.Refresh();
        }

        private void button2tagTitle_Click(object sender, EventArgs e)
        {
            Copy_FileName_2_tagTitle();
            //
            dataGridView1.Refresh();
        }

        private void button_x00_Click(object sender, EventArgs e)
        {
            if (0 == (Control.ModifierKeys & Keys.Shift))
            {
                MultiplyTrackNumber(0, ((Button)sender).Text);
            }
            else
            {
                MultiplyTrackNumber(4, ((Button)sender).Text);
            }
            checkBox_RemoveCommon.Checked = false;
            isTrackMuliplied = false;
        }

        private void buttonRenumberTracks_Click(object sender, EventArgs e)
        {
            RenumberTracks();
        }

        private void checkBox_RemoveCommon_CheckedChanged(object sender, EventArgs e)
        {
            //
            if (checkBox_RemoveCommon.Checked)// false->true
            {
                checkBox_RemoveCommon.ForeColor = checkBox_UpdateTags.ForeColor;
                checkBox_RemoveCommon.BackColor = checkBox_UpdateTags.BackColor;
            }
            else//true->false
            {
                checkBox_RemoveCommon.ForeColor = Color.White;
                checkBox_RemoveCommon.BackColor = Color.Crimson;
                //Cursor.Position = checkBox_RemoveCommon.PointToScreen(new Point(checkBox_RemoveCommon.Width / 2, checkBox_RemoveCommon.Height / 2));
            }
        }

        private void dataGridView1_Scroll(object sender, ScrollEventArgs e)
        {
            // po przejściu na VS2010 powoduje bardzo silne migotanie ekranu
            //if (e.ScrollOrientation == ScrollOrientation.HorizontalScroll)
            {
                //dataGridView1.Refresh();// przy scrolowaniu suwakiem w poziomie czesto obraz się psuje
            }

        }

        private void contextMenuStrip1_Opening(object sender, CancelEventArgs e)
        {
            if ((-1 == dragRow) && (-1 == dragCol))
            {
                e.Cancel = true;
                return;
            }
            //
            if (0 == LD.Count)
            {
                e.Cancel = true;
                return;
            }
            //
            switch (CData.ColumnName(dragCol))
            {
                case "tagArtist":
                    //
                    selectContextMenuPart(ContextMenuKind.TextOperations);
                    //
                    break;
                case "FileName":
                case "tagTitle":
                case "tagAlbum":
                    //
                    selectContextMenuPart(ContextMenuKind.TextOperations);
                    //
                    break;
                case "tagTrack"://***
                    if (-1 == dragRow)// nagłówku tylko zerowanie
                    {
                        //
                        selectContextMenuPart(ContextMenuKind.TrackNumbersOperations);
                        //
                        return;
                    }
                    //
                    if (!Utils.isTrackNumberEmpty(dataGridView1.Rows[dragRow].Cells["tagTrackDataGridViewTextBoxColumn"].Value.ToString()))
                    {
                        // działa tylko w pustym polu
                        e.Cancel = true;
                        //
                        return;
                    }
                    //
                    selectContextMenuPart(ContextMenuKind.NextTrackNumber);
                    //
                    break;
                default:
                    e.Cancel = true;
                    return;
            }
            //
        }

        private void sentenceCaseToolStripMenuItem_Click(object sender, EventArgs e)
        {
            int rowMin, rowMax;
            if (-1 == dragRow)
            {
                rowMin=0;
                rowMax = LD.Count;
            }
            else
            {
                rowMin = dragRow;
                rowMax = rowMin + 1;
            }
            //
            //string s = dataGridView1.Rows[dragRow].Cells[dragCol].Value.ToString().Trim();
            //
            changeCase(dragCol, rowMin, rowMax, CaseUtils.CASE.SentenceCase);
        }

        private void lowercaseToolStripMenuItem_Click(object sender, EventArgs e)
        {
            int rowMin, rowMax;
            if (-1 == dragRow)
            {
                rowMin = 0;
                rowMax = LD.Count;
            }
            else
            {
                rowMin = dragRow;
                rowMax = rowMin + 1;
            }
            //
            changeCase(dragCol, rowMin, rowMax, CaseUtils.CASE.lowercase);
        }

        private void uPPERCASEToolStripMenuItem_Click(object sender, EventArgs e)
        {
            int rowMin, rowMax;
            if (-1 == dragRow)
            {
                rowMin = 0;
                rowMax = LD.Count;
            }
            else
            {
                rowMin = dragRow;
                rowMax = rowMin + 1;
            }
            //
            changeCase(dragCol, rowMin, rowMax, CaseUtils.CASE.UPPERCASE);
        }

        private void capitalizeEachWordToolStripMenuItem_Click(object sender, EventArgs e)
        {
            int rowMin, rowMax;
            if (-1 == dragRow)
            {
                rowMin = 0;
                rowMax = LD.Count;
            }
            else
            {
                rowMin = dragRow;
                rowMax = rowMin + 1;
            }
            //
            changeCase(dragCol, rowMin, rowMax, CaseUtils.CASE.CapitalizeEachWord);
        }

        private void tOGGLECASEToolStripMenuItem_Click(object sender, EventArgs e)
        {
            int rowMin, rowMax;
            if (-1 == dragRow)
            {
                rowMin = 0;
                rowMax = LD.Count;
            }
            else
            {
                rowMin = dragRow;
                rowMax = rowMin + 1;
            }
            //
            changeCase(dragCol, rowMin, rowMax, CaseUtils.CASE.tOGGLEcASE);
        }

        private void SwapLastFirstToolStripMenuItem_Click(object sender, EventArgs e)
        {
            int rowMin, rowMax;
            if (-1 == dragRow)
            {
                rowMin = 0;
                rowMax = LD.Count;
            }
            else
            {
                rowMin = dragRow;
                rowMax = rowMin + 1;
            }
            //
            changeCase(dragCol, rowMin, rowMax, CaseUtils.CASE.SwapLastFirst);
        }

        private void nextNumberToolStripMenuItem_Click(object sender, EventArgs e)
        {
            string trackNo = dataGridView1.Rows[dragRow].Cells["tagTrackDataGridViewTextBoxColumn"].Value.ToString();
            //
            if (Utils.isTrackNumberEmpty(trackNo))
            {
                int n = getMaxTrackNumber()+1;
                dataGridView1.Rows[dragRow].Cells["tagTrackDataGridViewTextBoxColumn"].Value = GetLegalTrackNumber(n.ToString());
                dataGridView1.Rows[dragRow].Cells["tagTrackChangedDataGridViewCheckBoxColumn"].Value = true;
                dataGridView1.Rows[dragRow].Cells["markerDataGridViewCheckBoxColumn"].Value = true;
                checkBox_UpdateTags.Checked = true;
            }
        }

        private void clearAllNumbersToolStripMenuItem_Click(object sender, EventArgs e)
        {
            for (int k = 0; k < LD.Count; k++)
            {
                dataGridView1.Rows[k].Cells["tagTrackDataGridViewTextBoxColumn"].Value = GetLegalTrackNumber("");
                dataGridView1.Rows[k].Cells["tagTrackChangedDataGridViewCheckBoxColumn"].Value = true;
                dataGridView1.Rows[k].Cells["markerDataGridViewCheckBoxColumn"].Value = true;
            }
            checkBox_UpdateTags.Checked = true;
        }

        private void renumberToolStripMenuItem_Click(object sender, EventArgs e)
        {
            RenumberTracks();
        }

        private void dataGridView1_DataError(object sender, DataGridViewDataErrorEventArgs e)
        {
            string s=e.Exception.Message;
            return;
        }

        //
    }//class
    //---------------------------------------------------------------------------------------------------------------------------------
    //---------------------------------------------------------------------------------------------------------------------------------
    //---------------------------------------------------------------------------------------------------------------------------------
}//namespace
